<?php

namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use App\Events\PlaceOrder;

use Illuminate\Routing\Controller as BaseController;

class UsersController extends BaseController
{
	public function index()
	{
		echo 'index';
	}

	public function show()
	{
		echo 'show';
	}

	public function getUserByEmail($email)
	{
		echo 'User:'.$email->name;
	}

	public function db()
	{
		// $list = DB::table('users')->get();

		$list = DB::table('users')
					

					// ->where(function ($query) {
					// 	$query->where('key','!=','value')->orWhere()
					// })


					// ->when($id , function ($query) use ($id) {
					// 	return $query->where();
					// })


					->paginate(2);

		// return $list;

		DB::table('users')->insertOrIgnore([
		    ['email' => 'picard@example.com', 'name' => 'picard', 'password'=>'123456'],
		    ['email' => 'janeway@example.com', 'name' => 'janeway', 'password'=>'123456'],
		]);

		// insertGetId 返回自增ID
		// insertOrIgnore 自增值会增长

		DB::table('users')->where('id', 2)
			->update(['email' => 'picard@163.com']);

		// DB::table('users')
		//     ->updateOrInsert(
		//         ['email' => 'john@exampleaaaaa.com', 'name' => 'John', 'password'=>'123456'], // 匹配
		//         ['email' => 'john@examplebbbbb.com'] // 更新
		//     );

		// DB::begintranstion();

	    DB::table('users')->upsert([
		    ['email' => 'Oakland', 'name' => 'San Diego', 'password' => 99999],
		    ['email' => 'Chicago', 'name' => 'New York', 'password' => 150000]
		], ['email'], ['password']);

		// DB::rollback()

		// DB::commit();


		return $list;
	}


	public function orm()
	{
		$user = new User();
		$data = $user->select('*')
			// ->when(true, function () {

			// })->where()
			->orderBy('name', 'desc')
			->get();

		return $data;
	}


	public function placeOrder()
	{
		$user = User::find(10);

		PlaceOrder::dispatch($user);

		echo 'ok';
	}
}